added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / CSASPNETHighlightCodeInPage / HighlightCodePage.aspx.cs
blob9d85ef6efbb0f056b296375313500314c0c3728f
1 /**************************** Module Header ********************************\
2 * Module Name: HighlightCodePage.aspx.cs
3 * Project: CSASPNETHighlightCodeInPage
4 * Copyright (c) Microsoft Corporation
6 * Sometimes we input code like C# or HTML in our post and we need these code
7 * to be highlighted for a better reading experience.The project illustrates how
8 * to highlight the code in a page.
10 * This page is used to let user highlight the code.
12 * This source is subject to the Microsoft Public License.
13 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
14 * All other rights reserved.
16 \***************************************************************************/
18 using System;
19 using System.Web.UI.WebControls;
20 using System.Collections;
22 namespace CSASPNETHighlightCodeInPage
24 public partial class HighlightCodePage : System.Web.UI.Page
26 protected void Page_Load(object sender, EventArgs e)
28 this.lbError.Visible = false;
29 this.lbResult.Visible = false;
32 protected void btnHighLight_Click(object sender, EventArgs e)
34 string _error = string.Empty;
36 // Check the value of user's input data.
37 if (CheckControlValue(this.ddlLanguage.SelectedValue,
38 this.tbCode.Text, out _error))
40 // Initialize the Hashtable variable which used to
41 // store the different languages of code and their
42 // related regular expressions with matching options.
43 Hashtable _htb = CodeManager.Init();
45 // Initialize the suitable collection object.
46 RegExp _rg = new RegExp();
47 _rg = (RegExp)_htb[this.ddlLanguage.SelectedValue];
48 this.lbResult.Visible = true;
49 if (this.ddlLanguage.SelectedValue != "html")
51 // Display the highlighted code in a label control.
52 this.lbResult.Text = CodeManager.Encode(
53 CodeManager.HighlightCode(
54 Server.HtmlEncode(this.tbCode.Text)
55 .Replace(""", "\""),
56 this.ddlLanguage.SelectedValue, _rg)
59 else
61 // Display the highlighted code in a label control.
62 this.lbResult.Text = CodeManager.Encode(
63 CodeManager.HighlightHTMLCode(this.tbCode.Text, _htb)
67 else
69 this.lbError.Visible = true;
70 this.lbError.Text = _error;
73 public bool CheckControlValue(string selectValue,
74 string inputValue,
75 out string error)
77 error = string.Empty;
78 if (selectValue == "-1")
80 error = "Please choose the language.";
81 return false;
83 if (string.IsNullOrEmpty(inputValue))
85 error = "Please paste your code in the textbox control.";
86 return false;
88 return true;